package Stack;

import java.util.Stack;

/**
 * @author zhaoziqiang
 * @Description: 字符串括号匹配
 * @date 2020/8/17 14:45
 */
public class Solution {
//    public static boolean isValid(String s) {
//        Stack<Character> stack = new Stack<>();
//        for (int i = 0; i < s.length(); i++) {
//            char c = s.charAt(i);
//            if (c == '(' || c == '{' || c == '[') {
//                stack.push(c);
//            } else {
//                if (stack.isEmpty())
//                    return false;
//                Character pop = stack.pop();
//                if (c == ')' && pop != '(')
//                    return false;
//                if (c == ']' && pop != '[')
//                    return false;
//                if (c == '}' && pop != '{')
//                    return false;
//            }
//        }
//        return stack.isEmpty();
//    }
public static boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    char[] chars = s.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if(chars[i]=='(') stack.push(')');
        else if(chars[i]=='[') stack.push(']');
        else if(chars[i]=='{') stack.push('}');
        else if(stack.isEmpty()||stack.pop()!=chars[i]) return false;
    }
    return stack.isEmpty();
}

    public static void main(String[] args) {
        System.out.println(isValid("[](){}"));
        System.out.println(isValid("([{}])"));
        System.out.println(isValid("([)]"));
    }
}
